package tool.prototypes;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import japa.parser.JavaParser;
import japa.parser.ParseException;
import japa.parser.ast.CompilationUnit;

	/**
	 * @author jamie stevenson
	 * @date 2013/11/01
	 * 
	 * A primitive class to try out java parser - will need to be applicationised if
	 * used for any length of time.
	 *
	 */

public class RunFileParser {

	public static void main (String[] args) {
		
		//File location = new File("C:"+File.separatorChar+"Users"+File.separatorChar+"jwb09119"+File.separatorChar+"Desktop"+File.separatorChar+"javaparser-1.0.8-src"+File.separatorChar);
		File location = new File ("C:"+File.separatorChar+"Users"+File.separatorChar+"jwb09119"+File.separatorChar+"Desktop"+File.separatorChar+"ArgoUML-0-28-1"+File.separatorChar+"ArgoUML-0-28-1"+File.separatorChar+"src"+File.separatorChar);
		//File location = new File ("S:"+File.separatorChar+"system"+File.separatorChar+"Windows"+File.separatorChar+"Desktop"+File.separatorChar+"workspace"+File.separatorChar+"bitten"+File.separatorChar+"src"+File.separatorChar);
		
		if (!location.isDirectory()) {
			System.out.println("This location is not a directory!");
			System.exit(1);
		}
		
		//File[] files = location.listFiles();
		//System.out.println("Files found: "+ files.length);
		
		List<File> files = listJavaFiles(location);
		System.out.println("Files found: "+ files.size());
		
		CompilationUnit unit = null;
		
		for(File thisFile : files) {
			
			System.out.println("Trying to parse: "+thisFile.toString());
		
			try (FileInputStream in = new FileInputStream(thisFile)){
				unit = JavaParser.parse(in);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			if (unit!= null) {
				
				System.out.println("Parsed Object:");
				System.out.println("\t Name: " + unit.getClass().getClass().getSimpleName());
				System.out.println("\t Canonical Name: " + unit.getClass().getCanonicalName());
				
				System.out.println("Package: "+unit.getPackage());
				
//				int interfaces = unit.getClass().getClass().getInterfaces().length;
//				if (interfaces > 4) {
//					
//					System.out.println("\t Interfaces: " + unit.getClass().getClass().getInterfaces().length);
//					System.out.println("\t Modifiers: " + unit.getClass().getModifiers());
//					System.out.println("\t Types: ");
//					java.lang.reflect.Type[] ifaces = unit.getClass().getClass().getInterfaces();
//					
//					for (java.lang.reflect.Type t : ifaces) {
//						
//						System.out.println("\t \t " + t.toString());
//					
//					}
//				
//				}	
				
			}
		
		}
		System.out.println();
		System.out.println("All Done");
	}
	
	
	private static List<File> listJavaFiles (File location) {
		
		List<File> reply = new ArrayList<File>();
		
		if (location.isFile() && location.getName().endsWith(".java")) {
			
			reply.add(location);
			
		} else {
			
			// Handle a sub-directory
			if (location.isDirectory()) {
				
				File[] nextFiles = location.listFiles();
				
				if (nextFiles != null) {
					
					for (File newFile : nextFiles) {
						
						reply.addAll(listJavaFiles(newFile));
						
					}
					
				}
				
				
			}
			
		}
		
		return reply;
		
	}
	
	
}
